/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fri.cbw.MyTestModelTool1;

import fri.cbw.CBWutil.CBWUtil;
import fri.cbw.GenericTool.AbstractGenericTool;
import fri.cbw.GenericTool.AbstractModelTool;
import fri.cbw.GenericTool.AbstractReaction;
import fri.cbw.GenericTool.AbstractSpecies;
import fri.cbw.GenericTool.ToolTopComponent;
import fri.cbw.GenericTool.Unit;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;
import org.openide.awt.ActionID;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.TopComponent;

/**
 * Top component which displays something.
 */

@TopComponent.Description(
    preferredID = "MyTestModelToolTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_NEVER)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "fri.cbw.MyTestModelTool1.MyTestModelToolTopComponent")
//*@ActionReference(path = "Menu/Window" /*, position = 333 */)

@Messages({
    "CTL_MyTestModelToolAction=MyTestModelTool",
    "CTL_MyTestModelToolTopComponent=MyTestModelTool Window",
    "HINT_MyTestModelToolTopComponent=This is a MyTestModelTool window"
})
public final class MyTestModelToolTopComponent extends ToolTopComponent {
    
    private DefaultTableModel speciesTableModel = new javax.swing.table.DefaultTableModel( new String [] { "Species" }, 0 );
    private DefaultTableModel reactionsTableModel = new javax.swing.table.DefaultTableModel(new String [] { "Reactants", "K (on)", "K (off)", "Products" }, 0);
    private TestModulReactionImpl newReaction;
    
    public MyTestModelToolTopComponent(AbstractGenericTool gt) {
        super(gt);
        initComponents();
        setName(Bundle.CTL_MyTestModelToolTopComponent());
        setToolTipText(Bundle.HINT_MyTestModelToolTopComponent());
        
        evaluateComponents();
    }
    
    private void evaluateComponents() {
        populateSpeciesTable();
        populateReactionsTable();
    }
    
    private void populateSpeciesTable() {
        /* clear the table */
        speciesTableModel = new javax.swing.table.DefaultTableModel( new String [] { "Species" }, 0 );
        jTableSpecies.setModel(speciesTableModel);
        
        
        /* filling the species table */
        LinkedHashMap<AbstractSpecies, Double> species = ((AbstractModelTool)getGenericTool()).getSpecies();
        if(species != null){
            for (AbstractSpecies specie : species.keySet()) {
                speciesTableModel.addRow( new AbstractSpecies[] {specie});
            }
        }
    }
    
    private void populateReactionsTable() {
        
        /* clear the table */
        for(int i = 0; i < reactionsTableModel.getRowCount(); i++){
            reactionsTableModel.removeRow(i);
        }
        
        /* filling the reactions table */
        List<AbstractReaction> reactions = ((AbstractModelTool)getGenericTool()).getReactions();
        if(reactions != null){
            for (AbstractReaction reaction : reactions) {
                TestModulReactionImpl tmr  = ((TestModulReactionImpl)reaction);
                
                reactionsTableModel.addRow(new Object []{
                    tmr.reactantsToString(), 
                    tmr.getkOn(), 
                    tmr.getkOff(), 
                    tmr.productsToString()});
            }
        }
    }
    
    
    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT
     * modify this code. The content of this method is always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialogAddReaction = new javax.swing.JDialog();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldProductsSum = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldReactantsSum = new javax.swing.JTextField();
        jComboBoxSpecies1 = new javax.swing.JComboBox();
        jTextFieldProducts = new javax.swing.JTextField();
        btnAddProduct = new javax.swing.JButton();
        jComboBoxSpecies2 = new javax.swing.JComboBox();
        jTextFieldReactants = new javax.swing.JTextField();
        jButton6 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabelKOn = new javax.swing.JLabel();
        jTextFieldKOn = new javax.swing.JTextField();
        jComboBoxKOn = new javax.swing.JComboBox();
        jCheckBoxReversible = new javax.swing.JCheckBox();
        jLabelKOff = new javax.swing.JLabel();
        jTextFieldKOff = new javax.swing.JTextField();
        jComboBoxKOff = new javax.swing.JComboBox();
        jButtonDialogCancle = new javax.swing.JButton();
        jButtonDialogOK = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jDialogAddSpecies = new javax.swing.JDialog();
        jLabel3 = new javax.swing.JLabel();
        fieldNewSpeciesName = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        btnNewSpeciesAdd = new javax.swing.JButton();
        btnNewSpeciesCancel = new javax.swing.JButton();
        jButtonAddSpecies = new javax.swing.JButton();
        jButtonDeleteSpecies = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTableReactions = new javax.swing.JTable();
        jButtonDeleteReaction = new javax.swing.JButton();
        btnAddReaction = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTableSpecies = new javax.swing.JTable();

        jDialogAddReaction.setTitle(org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jDialogAddReaction.title")); // NOI18N
        jDialogAddReaction.setMinimumSize(new java.awt.Dimension(540, 300));
        jDialogAddReaction.setModal(true);
        jDialogAddReaction.setResizable(false);

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jLabel1.text")); // NOI18N

        jTextFieldProductsSum.setEditable(false);
        jTextFieldProductsSum.setText(org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jTextFieldProductsSum.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jLabel2.text")); // NOI18N

        jTextFieldReactantsSum.setEditable(false);
        jTextFieldReactantsSum.setText(org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jTextFieldReactantsSum.text")); // NOI18N

        jTextFieldProducts.setText(org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jTextFieldProducts.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(btnAddProduct, org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.btnAddProduct.text")); // NOI18N
        btnAddProduct.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddProductActionPerformed(evt);
            }
        });

        jTextFieldReactants.setText(org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jTextFieldReactants.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jButton6, org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jButton6.text")); // NOI18N
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabelKOn, org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jLabelKOn.text")); // NOI18N

        jTextFieldKOn.setText(org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jTextFieldKOn.text")); // NOI18N

        jComboBoxKOn.setModel(getUnits());
        jComboBoxKOn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxKOnActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jCheckBoxReversible, org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jCheckBoxReversible.text")); // NOI18N
        jCheckBoxReversible.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxReversibleActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabelKOff, org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jLabelKOff.text")); // NOI18N
        jLabelKOff.setEnabled(false);

        jTextFieldKOff.setText(org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jTextFieldKOff.text")); // NOI18N
        jTextFieldKOff.setEnabled(false);

        jComboBoxKOff.setModel(getUnits());
        jComboBoxKOff.setEnabled(false);

        org.openide.awt.Mnemonics.setLocalizedText(jButtonDialogCancle, org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jButtonDialogCancle.text")); // NOI18N
        jButtonDialogCancle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDialogCancleActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jButtonDialogOK, org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jButtonDialogOK.text")); // NOI18N
        jButtonDialogOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDialogOKActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jLabel5.text")); // NOI18N

        javax.swing.GroupLayout jDialogAddReactionLayout = new javax.swing.GroupLayout(jDialogAddReaction.getContentPane());
        jDialogAddReaction.getContentPane().setLayout(jDialogAddReactionLayout);
        jDialogAddReactionLayout.setHorizontalGroup(
            jDialogAddReactionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialogAddReactionLayout.createSequentialGroup()
                .addGroup(jDialogAddReactionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jDialogAddReactionLayout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(jDialogAddReactionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator1)
                            .addGroup(jDialogAddReactionLayout.createSequentialGroup()
                                .addGroup(jDialogAddReactionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel1)
                                    .addGroup(jDialogAddReactionLayout.createSequentialGroup()
                                        .addComponent(jComboBoxSpecies1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jTextFieldProducts, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(btnAddProduct))
                                    .addComponent(jTextFieldProductsSum))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jDialogAddReactionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel2)
                                    .addGroup(jDialogAddReactionLayout.createSequentialGroup()
                                        .addComponent(jComboBoxSpecies2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jTextFieldReactants, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButton6))
                                    .addComponent(jTextFieldReactantsSum)))
                            .addGroup(jDialogAddReactionLayout.createSequentialGroup()
                                .addGroup(jDialogAddReactionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jCheckBoxReversible)
                                    .addGroup(jDialogAddReactionLayout.createSequentialGroup()
                                        .addGroup(jDialogAddReactionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabelKOn)
                                            .addGroup(jDialogAddReactionLayout.createSequentialGroup()
                                                .addComponent(jTextFieldKOn, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18, 18)
                                                .addComponent(jComboBoxKOn, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGap(68, 68, 68)
                                        .addGroup(jDialogAddReactionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(jDialogAddReactionLayout.createSequentialGroup()
                                                .addComponent(jTextFieldKOff, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18, 18)
                                                .addComponent(jComboBoxKOff, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addComponent(jLabelKOff)))
                                    .addComponent(jLabel5))
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDialogAddReactionLayout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonDialogOK)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonDialogCancle)))
                .addContainerGap())
        );

        jDialogAddReactionLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jComboBoxSpecies1, jComboBoxSpecies2});

        jDialogAddReactionLayout.setVerticalGroup(
            jDialogAddReactionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialogAddReactionLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jDialogAddReactionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jDialogAddReactionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldProductsSum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldReactantsSum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jDialogAddReactionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxSpecies1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldProducts, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBoxSpecies2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldReactants, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAddProduct)
                    .addComponent(jButton6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jDialogAddReactionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelKOn)
                    .addComponent(jLabelKOff))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jDialogAddReactionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jComboBoxKOn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBoxKOff, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldKOff, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldKOn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jCheckBoxReversible)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jDialogAddReactionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonDialogCancle)
                    .addComponent(jButtonDialogOK))
                .addContainerGap())
        );

        jLabel1.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jLabel1.AccessibleContext.accessibleName")); // NOI18N

        jDialogAddSpecies.setAlwaysOnTop(true);
        jDialogAddSpecies.setMinimumSize(new java.awt.Dimension(210, 130));
        jDialogAddSpecies.setResizable(false);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        org.openide.awt.Mnemonics.setLocalizedText(jLabel3, org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jLabel3.text")); // NOI18N

        fieldNewSpeciesName.setText(org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.fieldNewSpeciesName.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel4, org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jLabel4.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(btnNewSpeciesAdd, org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.btnNewSpeciesAdd.text")); // NOI18N
        btnNewSpeciesAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewSpeciesAddActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btnNewSpeciesCancel, org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.btnNewSpeciesCancel.text")); // NOI18N
        btnNewSpeciesCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewSpeciesCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jDialogAddSpeciesLayout = new javax.swing.GroupLayout(jDialogAddSpecies.getContentPane());
        jDialogAddSpecies.getContentPane().setLayout(jDialogAddSpeciesLayout);
        jDialogAddSpeciesLayout.setHorizontalGroup(
            jDialogAddSpeciesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialogAddSpeciesLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jDialogAddSpeciesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addGroup(jDialogAddSpeciesLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jDialogAddSpeciesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnNewSpeciesAdd)
                            .addComponent(fieldNewSpeciesName, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnNewSpeciesCancel)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jDialogAddSpeciesLayout.setVerticalGroup(
            jDialogAddSpeciesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialogAddSpeciesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jDialogAddSpeciesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fieldNewSpeciesName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addContainerGap(55, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDialogAddSpeciesLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jDialogAddSpeciesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNewSpeciesCancel)
                    .addComponent(btnNewSpeciesAdd))
                .addContainerGap())
        );

        jLabel4.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jLabel4.AccessibleContext.accessibleName")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jButtonAddSpecies, org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jButtonAddSpecies.text")); // NOI18N
        jButtonAddSpecies.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddSpeciesActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jButtonDeleteSpecies, org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jButtonDeleteSpecies.text")); // NOI18N
        jButtonDeleteSpecies.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteSpeciesActionPerformed(evt);
            }
        });

        jTableReactions.setModel(reactionsTableModel);
        jScrollPane2.setViewportView(jTableReactions);
        jTableReactions.getColumnModel().getColumn(0).setHeaderValue(org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jTableReactions.columnModel.title0_2")); // NOI18N
        jTableReactions.getColumnModel().getColumn(1).setHeaderValue(org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jTableReactions.columnModel.title1_2")); // NOI18N
        jTableReactions.getColumnModel().getColumn(2).setHeaderValue(org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jTableReactions.columnModel.title2_2")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jButtonDeleteReaction, org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.jButtonDeleteReaction.text")); // NOI18N
        jButtonDeleteReaction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDeleteReactionActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(btnAddReaction, org.openide.util.NbBundle.getMessage(MyTestModelToolTopComponent.class, "MyTestModelToolTopComponent.btnAddReaction.text")); // NOI18N
        btnAddReaction.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddReactionActionPerformed(evt);
            }
        });

        jTableSpecies.setModel(speciesTableModel);
        jScrollPane3.setViewportView(jTableSpecies);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(83, 83, 83)
                        .addComponent(jButtonAddSpecies)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonDeleteSpecies)
                        .addGap(329, 329, 329)
                        .addComponent(btnAddReaction)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonDeleteReaction)))
                .addContainerGap(173, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnAddReaction)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButtonAddSpecies)
                        .addComponent(jButtonDeleteSpecies)
                        .addComponent(jButtonDeleteReaction)))
                .addGap(178, 178, 178))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    
    
     
    /**
     * This method deletes a specie from the table
     * @param evt 
     */
    private void jButtonDeleteSpeciesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteSpeciesActionPerformed
        int row = jTableSpecies.getSelectedRow();
        
        if(row >= 0) {
            ((AbstractModelTool)getGenericTool()).getSpecies().remove(
                    (AbstractSpecies) speciesTableModel.getValueAt(row, 0));
            speciesTableModel.removeRow(row);
        }
    }//GEN-LAST:event_jButtonDeleteSpeciesActionPerformed
    
    private void btnAddReactionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddReactionActionPerformed
        clearDialog();
        
        newReaction = new TestModulReactionImpl();
        
        AbstractSpecies[] items = ((AbstractModelTool)getGenericTool()).getSpecies().keySet().toArray(new AbstractSpecies[0]);
        jComboBoxSpecies1.setModel( new javax.swing.DefaultComboBoxModel(items) );
        jComboBoxSpecies2.setModel( new javax.swing.DefaultComboBoxModel(items) );
        
        jDialogAddReaction.setVisible(true);
        
    }//GEN-LAST:event_btnAddReactionActionPerformed
    
    private void jButtonAddSpeciesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddSpeciesActionPerformed
        /* clear dialog */
        fieldNewSpeciesName.setText("");
        
        jDialogAddSpecies.setVisible(true);
    }//GEN-LAST:event_jButtonAddSpeciesActionPerformed

    private void btnAddProductActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddProductActionPerformed
        
        Double quantity = Double.parseDouble(jTextFieldProducts.getText());
        newReaction.addProduct((AbstractSpecies)jComboBoxSpecies1.getSelectedItem(), quantity);
        
        jTextFieldProductsSum.setText(newReaction.productsToString());
    }//GEN-LAST:event_btnAddProductActionPerformed

    private void jComboBoxKOnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxKOnActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBoxKOnActionPerformed

    private void jCheckBoxReversibleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxReversibleActionPerformed
        setKoff(jCheckBoxReversible.isSelected());
    }//GEN-LAST:event_jCheckBoxReversibleActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        Double quantity = Double.parseDouble(jTextFieldReactants.getText());
        newReaction.addReactant((AbstractSpecies)jComboBoxSpecies2.getSelectedItem(), quantity);
        
        jTextFieldReactantsSum.setText(newReaction.reactantsToString());
        
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButtonDialogCancleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDialogCancleActionPerformed
        jDialogAddReaction.setVisible(false);
    }//GEN-LAST:event_jButtonDialogCancleActionPerformed

    private void jButtonDialogOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDialogOKActionPerformed
        
        Double kOn = CBWUtil.getDoubleZero(jTextFieldKOn.getText());
        Double kOff = CBWUtil.getDoubleZero(jTextFieldKOff.getText());
        
        newReaction.setkOn(kOn);
        newReaction.setkOnUnit(Unit.MOL);
        newReaction.setkOff(kOff);
        newReaction.setkOffUnit(Unit.MOL);
        newReaction.setReversible(jCheckBoxReversible.isSelected());
        
        reactionsTableModel.addRow(new Object []{ 
            jTextFieldReactantsSum.getText(), 
            kOn, 
            kOff,
            jTextFieldProductsSum.getText()});
        jTableReactions.setModel(reactionsTableModel);
        
        jDialogAddReaction.setVisible(false);
    }//GEN-LAST:event_jButtonDialogOKActionPerformed

    private void jButtonDeleteReactionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonDeleteReactionActionPerformed
        int row = jTableReactions.getSelectedRow();
        
        if(row >= 0) {
            reactionsTableModel.removeRow(row);
        }
    }//GEN-LAST:event_jButtonDeleteReactionActionPerformed

    private void btnNewSpeciesAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewSpeciesAddActionPerformed
        // Dodamo novo vrsto
        ((AbstractModelTool)getGenericTool()).addSpecies(fieldNewSpeciesName.getText());
        
        //Zapremo dialog
        jDialogAddSpecies.setVisible(false);
        
        //Osvežimo tabelo z vrstami
        populateSpeciesTable();
    }//GEN-LAST:event_btnNewSpeciesAddActionPerformed

    private void btnNewSpeciesCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewSpeciesCancelActionPerformed
        jDialogAddSpecies.setVisible(false);
    }//GEN-LAST:event_btnNewSpeciesCancelActionPerformed
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddProduct;
    private javax.swing.JButton btnAddReaction;
    private javax.swing.JButton btnNewSpeciesAdd;
    private javax.swing.JButton btnNewSpeciesCancel;
    private javax.swing.JTextField fieldNewSpeciesName;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButtonAddSpecies;
    private javax.swing.JButton jButtonDeleteReaction;
    private javax.swing.JButton jButtonDeleteSpecies;
    private javax.swing.JButton jButtonDialogCancle;
    private javax.swing.JButton jButtonDialogOK;
    private javax.swing.JCheckBox jCheckBoxReversible;
    private javax.swing.JComboBox jComboBoxKOff;
    private javax.swing.JComboBox jComboBoxKOn;
    private javax.swing.JComboBox jComboBoxSpecies1;
    private javax.swing.JComboBox jComboBoxSpecies2;
    private javax.swing.JDialog jDialogAddReaction;
    private javax.swing.JDialog jDialogAddSpecies;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabelKOff;
    private javax.swing.JLabel jLabelKOn;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTableReactions;
    private javax.swing.JTable jTableSpecies;
    private javax.swing.JTextField jTextFieldKOff;
    private javax.swing.JTextField jTextFieldKOn;
    private javax.swing.JTextField jTextFieldProducts;
    private javax.swing.JTextField jTextFieldProductsSum;
    private javax.swing.JTextField jTextFieldReactants;
    private javax.swing.JTextField jTextFieldReactantsSum;
    // End of variables declaration//GEN-END:variables
    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    private void clearDialog() {
        jTextFieldProductsSum.setText("");
        jTextFieldReactants.setText("");
        jTextFieldReactantsSum.setText("");
        jTextFieldReactants.setText("");
        jTextFieldKOn.setText("");
        jTextFieldKOff.setText("");
        jCheckBoxReversible.setSelected(false);
        setKoff(false);
    }

    private void setKoff(boolean enabled) {
        jComboBoxKOff.setEnabled(enabled);
        jTextFieldKOff.setEnabled(enabled);
        jLabelKOff.setEnabled(enabled);
    }

    @Override
    public void doSave() {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    private ComboBoxModel getUnits() {
        return new DefaultComboBoxModel(Unit.values());
    }
}
